package com.jyh.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.jyh.common.PublicField;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 设置公共字段自动赋值
 */
@Component
@Slf4j
public class MyHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        //属性名为createTime对其赋值为LocalDateTime.now()
        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("createUser", PublicField.getCurrentId());
        metaObject.setValue("updateUser", PublicField.getCurrentId());

//        log.info("Myhandler.in线程：{}",Thread.currentThread().getId());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("updateUser", PublicField.getCurrentId());

//        log.info("Myhandler.up线程：{}",Thread.currentThread().getId());
    }
}
